package com.example.pictureview;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.pictureview.adapter.VideoAdapter;
import com.example.pictureview.widget.BaseImageLoader;

import java.util.ArrayList;
import java.util.List;

import static android.provider.MediaStore.Video.Thumbnails.MINI_KIND;

public class VideoThumbnailActivity extends Activity {

    private ListView mListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_thumbnail);
        mListView = (ListView) findViewById(R.id.listview);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                VideoActivity.gotoVideoActivity(VideoThumbnailActivity.this, (String) parent.getAdapter().getItem(position));
            }
        });
        new VideoTask().execute((Void) null);
    }

    public static void gotoActivity(Context context) {
        Intent intent = new Intent(context, VideoThumbnailActivity.class);
        context.startActivity(intent);
    }

    class VideoTask extends AsyncTask<Void, Void, List<String>> {
        @Override
        protected List<String> doInBackground(Void... params) {
            final List<String> paths = new ArrayList<>();
            if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {

                Uri originalUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                ContentResolver cr = VideoThumbnailActivity.this.getApplicationContext().getContentResolver();
                Cursor cursor = cr.query(originalUri, null, null, null, null);
                if (cursor == null) {
                    return paths;
                }
                for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                    String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
                    paths.add(path);

                }
                cursor.close();

            }
            return paths;
        }

        @Override
        protected void onPostExecute(List<String> strings) {
            mListView.setAdapter(new VideoAdapter(VideoThumbnailActivity.this, new MyImageLoader(), strings));

        }
    }

    private static class MyImageLoader extends BaseImageLoader {

        @Override
        protected Bitmap getBitmapFormUrl(String url) {
            return ThumbnailUtils.createVideoThumbnail(url, MINI_KIND);
        }
    }

}
